package com.lzq.course.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzq.course.server.domain.Category;
import com.lzq.course.server.domain.CategoryExample;
import com.lzq.course.server.dto.CategoryDto;
import com.lzq.course.server.dto.PageDto;
import com.lzq.course.server.dto.ResponseDto;
import com.lzq.course.server.mapper.CategoryMapper;
import com.lzq.course.server.util.CopyUtil;
import com.lzq.course.server.util.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * example 这个文件很有用
     * 可以帮我们生成where order 查询
     * 不用单独写sql的增删改查
     */
    public void list(PageDto<CategoryDto> pageDto) {
        // 分页查询 页码不是从0开始
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());

        // 排序
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");

        // 查询
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);

        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        pageDto.setTotal(pageInfo.getTotal());

        List<CategoryDto> categoryDtoList = CopyUtil.copyList(categoryList, CategoryDto.class);

        pageDto.setList(categoryDtoList);
    }

    /**
     * 查询所有分类
     */
    public ResponseDto all() {
        ResponseDto responseDto = new ResponseDto();
        // 排序
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");

        // 查询
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);

        List<CategoryDto> categoryDtoList = CopyUtil.copyList(categoryList, CategoryDto.class);

        responseDto.setContent(categoryDtoList);
        return responseDto;
    }

    /**
     * 保存 修改
     * @param categoryDto
     */
    public void save(CategoryDto categoryDto) {
        //BeanUtils.copyProperties(categoryDto, category);
        Category copyCategory = CopyUtil.copy(categoryDto, Category.class);

        if (StringUtils.isEmpty(categoryDto.getId())) {
            // 新增
            // 八位的 id
            copyCategory.setId(UuidUtil.getShortUuid());

            categoryMapper.insert(copyCategory);
        } else {
            // 编辑
            categoryMapper.updateByPrimaryKey(copyCategory);
        }
    }

    /**
     * 删除
     * @param id
     */
    public void delete(String id) {
        deleteChildren(id);
        categoryMapper.deleteByPrimaryKey(id);
    }

    /**
     *  如果是删除的是父分类 删除子分类
     * @param id
     */
    public void deleteChildren(String id) {
        Category category = categoryMapper.selectByPrimaryKey(id);
        if ("00000000".equals(category.getParent())) {
            CategoryExample categoryExample = new CategoryExample();
            CategoryExample.Criteria criteria = categoryExample.createCriteria();
            criteria.andParentEqualTo(category.getId());
            // 批量删除
            categoryMapper.deleteByExample(categoryExample);
        }
    }
}
